package com.yanqu.road.server.manager.activity.suitang.manager;

import com.yanqu.road.entity.activity.suitang.config.SuiTangCityConfig;
import com.yanqu.road.entity.activity.suitang.config.SuiTangConfigSync;
import com.yanqu.road.entity.activity.suitang.data.*;
import com.yanqu.road.entity.activity.suitang.entity.battle.CrossSuiTangBattlefield;
import com.yanqu.road.entity.activity.suitang.entity.battle.CrossSuiTangBattlefieldTeam;
import com.yanqu.road.entity.activity.suitang.entity.team.CrossSuiTangTeam;
import com.yanqu.road.entity.activity.suitang.entity.team.CrossSuiTangTeamArrive;
import com.yanqu.road.entity.activity.suitang.entity.union.CrossSuiTangUnionRound;
import com.yanqu.road.entity.activity.suitang.enums.eSuiTangCityType;
import com.yanqu.road.entity.activity.suitang.enums.eSuiTangBuffType;
import com.yanqu.road.entity.activity.suitang.enums.eSuiTangStage;
import com.yanqu.road.entity.activity.suitang.enums.eSuiTangTeamStatus;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.logic.activity.suitang.SuiTangMapLogic;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.manager.activity.suitang.task.impl.CrossSuiTangAutoBattleTask;
import com.yanqu.road.server.manager.activity.suitang.task.impl.CrossSuiTangMoveTask;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.string.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class CrossSuiTangMapActivityMgr extends CrossBaseActivityMgr {
    private Map<Long, Integer> nextMoveCity = new ConcurrentHashMap<>();

    protected static Logger logger = LogManager.getLogger(CrossSuiTangMapActivityMgr.class.getName());

    @Override
    public void init() {
        super.init();
        nextMoveCity.clear();
    }

    public boolean autoMoveToCity(CrossSuiTangTeam team, int toCityId) {
        if (team.getTargetCityId() == 0) {
            // 没有自动行军目标
            return false;
        }
        if (team.getTargetCityId() != toCityId) {
            // 自动行军目标不符
            return false;
        }
        int ret = activity.getBarracksActivityMgr().changeSoldier(team.getUserId(), team.getMaxSoldier());
        if (ret == 0) {
            activity.getSyncActivityMgr().syncBarracks(team.getUserId());
            activity.getSyncActivityMgr().syncTeamToSelf(team);
            ret = moveToCity(team, toCityId);
            activity.getSyncActivityMgr().syncAutoMoveFail(team.getUserId(), ret, false);
        } else {
            // 没兵了不再自动行军和自动战斗

            // 取消自动行军目标，而非自动战斗
//            team.setAutoMove(false);
            team.setTargetCityId(0);

            team.setAutoBattle(false);
            activity.getSyncActivityMgr().syncTeamToSelf(team);
            activity.getSyncActivityMgr().syncAutoMoveFail(team.getUserId(), ret, true);
        }
        return ret == 0;
    }

    public int moveToCity(CrossSuiTangTeam team, int toCityId) {
        if (activity == null) {
            return GameErrorCode.E_SUI_TANG_ACTIVITY_NOT_FOUND;
        }
        SuiTangConfigSync config = activity.getConfig();
        if (config == null || config.getCityConfigMap() == null || config.getCityConfigMap().isEmpty()) {
            return GameErrorCode.E_SUI_TANG_CONFIG_NOT_FOUND;
        }
        SuiTangCityConfig suiTangCityConfig = config.getCityConfigMap().get(toCityId);
        if (suiTangCityConfig == null) {
            return GameErrorCode.E_SUI_TANG_CITY_NO_EXIST;
        }

        if (team.getCityId() == toCityId) {
            synchronized (getPlayerLock(team.getUserId())) {
                if (team.isAutoMove()) {
                    team.setAutoBattle(true);
                    team.setTargetCityId(toCityId);
                    //activity.addTask(new CrossSuiTangAutoBattleTask(activity, System.currentTimeMillis(), getCity(team.getUserId()), getBattleTeam(getCity(team.getUserId()), team.getUserId())));
                } else {
                    team.setAutoBattle(false);
                    team.setTargetCityId(0);
                }
            }
            activity.getSyncActivityMgr().syncTeamToSelf(team);
            return 0;
        }

        // 时间判断
        if (!activity.isBattleStep()) {
            return GameErrorCode.E_SUI_TANG_NO_OPERATE_TIME;
        }

        // 是否淘汰
        if (isOut(team.getUserId())) {
            return GameErrorCode.E_SUI_TANG_IS_OUT;
        }

        // 城市归属判断
//        if (!getCity(team.getUserId()).getUnionId().equals(activity.getUserData(team.getUserId()).getUnionUid())) {
//            // 如果不在自己的城市，那么只能向己方城市移动
//            if (!getCurCity(team.getUserId(), toCityId).getUnionId().equals(activity.getUserData(team.getUserId()).getUnionUid())) {
//                return GameErrorCode.E_SUI_TANG_ONLY_GO_OUR_CITY;
//            }
//        }

        // 城池人数上限判断
        int ret = isLimitCityUserNum(team.getUserId(), toCityId);
        if (ret != 0) {
            return ret;
        }

        // 兵力判断
        if (activity.getCityActivityMgr().isHomeCity(getCity(team.getUserId()), activity.getUserData(team.getUserId()).getUnionUid()) && !soldierLimit(team)) {
            return GameErrorCode.E_SUI_TANG_TEAM_SOLDIER_MIN_LIMIT;
        }

        // 是否在保护时间
        if (activity.getCityActivityMgr().inProtectTime(getCurCity(team.getUserId(), toCityId))
                && !getCurCity(team.getUserId(), toCityId).getUnionId().equals(activity.getUserData(team.getUserId()).getUnionUid())) {
            return GameErrorCode.E_SUI_TANG_CITY_IN_PROTECT;
        }

        // 状态判断
        if (getTeamStatus(team) != eSuiTangTeamStatus.Free) {
            nextMoveCity.put(team.getUserId(), toCityId);
            return /*GameErrorCode.E_SUI_TANG_TEAM_CHANGE_SOLDIER_NO_FREE*/ 0;
        }

        // 城市判断
        int yinShan = 6001;
        int centerCity = 5001;
        if (toCityId == yinShan && !activity.getCityActivityMgr().canOpenYinShanCity(getCity(team.getUserId()))) {
            return GameErrorCode.E_SUI_TANG_CITY_NO_OPEN;
        }
        if (toCityId == centerCity && !activity.getCityActivityMgr().canOpenCenterCity()) {
            return GameErrorCode.E_SUI_TANG_CITY_NO_OPEN;
        }

        // 寻路
        List<Integer> cityRouteList = getRouteList(team, toCityId);
        if (cityRouteList == null || cityRouteList.size() <= 1) {
            return GameErrorCode.E_SUI_TANG_NO_ROUTE;
        }

        // 出发点不要
        // cityRouteList.remove(0);

        // 移动
        moveByRoute(team, cityRouteList);
        return 0;
    }

    public boolean soldierLimit(CrossSuiTangTeam team) {
        if (activity == null) {
            return false;
        }
        SuiTangConfigSync config = activity.getConfig();
        if (config == null || config.getCityConfigMap() == null || config.getCityConfigMap().isEmpty()) {
            return false;
        }

        // 兵力判断
        // 判断是否达到兵力最低要求,不满足的跳过处理
        // 千分比最低兵力和固定最低兵力取最大值
        long minSoldierNum = BigDecimal.valueOf(team.getMaxSoldier())
                .multiply(BigDecimal.valueOf(config.getParamConfig().getSuiTangTeamNumLowerLimitParam()))
                .divide(BigDecimal.valueOf(1000), 0, BigDecimal.ROUND_UP).longValue();
        if (team.getSoldierNum() < Math.max(minSoldierNum, config.getParamConfig().getSuiTangTeamNumLowerLimitFixedParam())) {
            return false;
        }
        return true;
    }

    public long minSoldierNum(CrossSuiTangTeam team) {
        if (activity == null) {
            return 0;
        }
        SuiTangConfigSync config = activity.getConfig();
        if (config == null || config.getCityConfigMap() == null || config.getCityConfigMap().isEmpty()) {
            return 0;
        }

        // 兵力判断
        // 判断是否达到兵力最低要求,不满足的跳过处理
        // 千分比最低兵力和固定最低兵力取最大值
        long minSoldierNum = BigDecimal.valueOf(team.getMaxSoldier())
                .multiply(BigDecimal.valueOf(config.getParamConfig().getSuiTangTeamNumLowerLimitParam()))
                .divide(BigDecimal.valueOf(1000), 0, BigDecimal.ROUND_UP).longValue();
        return minSoldierNum;
    }

    public int moveSpeedUp(long userId) {
        if (!activity.isAble(userId)) {
            return GameErrorCode.E_SUI_TANG_NO_ABLE;
        }

        long now = System.currentTimeMillis();
        CrossSuiTangUserData userData = activity.getUserData(userId);
        CrossSuiTangTeam team = getTeam(userId);
        if (team == null) {
            return GameErrorCode.E_SUI_TANG_PARAM_ERR;
        }
        SuiTangConfigSync config = activity.getConfig();
        if (config == null) {
            return GameErrorCode.E_SUI_TANG_CONFIG_NOT_FOUND;
        }
        // 时间判断
        if (!activity.isBattleStep()) {
            return GameErrorCode.E_SUI_TANG_NO_OPERATE_TIME;
        }

        int suiTangCityMoveTime = config.getParamConfig().getSuiTangCityMoveBuffCond();
        synchronized (getPlayerLock(team.getUserId())) {
            long arriveTime = team.getArriveTime();
            if (arriveTime - now <= suiTangCityMoveTime * DateHelper.SECOND_MILLIONS) {
                return GameErrorCode.E_SUI_TANG_SPEED_UP_LIMIT;
            }

            int buffEffect = getBuffEffect(config.getParamConfig().getSuiTangCityMoveBuffId());
            long reduce = BigDecimal.valueOf(arriveTime - now).multiply(BigDecimal.valueOf(buffEffect)).
                    divide(BigDecimal.valueOf(1000)).
                    setScale(0, BigDecimal.ROUND_DOWN).longValue();
            long reduceBegin = BigDecimal.valueOf(now - team.getBeginMoveTime()).multiply(BigDecimal.valueOf(buffEffect)).
                    divide(BigDecimal.valueOf(1000)).
                    setScale(0, BigDecimal.ROUND_DOWN).longValue();
            reduce = reduce < 0 ? 0 : reduce;
//        long beginMoveTime = BigDecimal.valueOf(now + reduce).
//                subtract(BigDecimal.valueOf(reduce).
//                        divide(BigDecimal.valueOf(1).
//                                subtract(BigDecimal.valueOf(now - team.getBeginMoveTime()).
//                                        divide(BigDecimal.valueOf(team.getArriveTime() - team.getBeginMoveTime()))))).
//                setScale(0, BigDecimal.ROUND_DOWN).longValue();
            team.setBeginMoveTime(now - reduceBegin);
            team.setArriveTime(now + reduce);
            team.setSpeedUpNum(team.getSpeedUpNum() + 1);
            team.setMoveUpdateTime(now);
            CrossSuiTangBattlefieldTeam battleTeam = getBattleTeam(getCity(userId), userId);
            CrossSuiTangTeamArrive battleTeamUser = battleTeam.getTeam();
            if (battleTeamUser != null) {
                battleTeamUser.setTime(now + reduce);
            }
            userData.setUpdateOption();
        }

        activity.getSyncActivityMgr().syncTeamToSelf(team);
        activity.getSyncActivityMgr().syncBattleResultMsg(getCity(team.getUserId()));
        return 0;
    }

    // 移动，开始移动表示必定可到达
    private void moveByRoute(CrossSuiTangTeam team, List<Integer> routeList) {
        // 队伍从旧城池移除
        int cityId = team.getCityId();
        Map<Integer, CrossSuiTangCity> curCityMap = getCurCityMap(team.getUserId());
        if (curCityMap == null || curCityMap.isEmpty()) {
            return;
        }
        CrossSuiTangCity city = curCityMap.get(cityId);
        if (city == null) {
            return;
        }
        SuiTangConfigSync config = activity.getConfig();
        if (config == null) {
            return;
        }
        synchronized (getCityLock(city)) {
            CrossSuiTangBattlefield battlefield = city.getBattlefield();
            if (battlefield == null) {
                return;
            }
            Map<Long, CrossSuiTangBattlefieldTeam> defTeamMap = battlefield.getDefTeamMap();
            if (defTeamMap == null) {
                return;
            }
            CrossSuiTangBattlefieldTeam arrive = defTeamMap.get(team.getUserId());
            if (arrive != null) {
                defTeamMap.remove(team.getUserId());
            }
            Map<Long, CrossSuiTangBattlefieldTeam> atkTeamMap = battlefield.getAtkTeamMap();
            if (atkTeamMap == null) {
                return;
            }
            CrossSuiTangBattlefieldTeam arriveAtk = atkTeamMap.get(team.getUserId());
            if (arriveAtk != null) {
                atkTeamMap.remove(team.getUserId());
            }
        }

        CrossSuiTangUserData user = activity.getUserData(team.getUserId());
        // 出发
        long now = System.currentTimeMillis();
        int toCityId = routeList.get(routeList.size() - 1);
        // 所需要的时间
        long t1 = calcMoveTime(routeList);
        // 到阴山特殊处理
        int yinShan = 6001;
        if (toCityId == yinShan || getCity(user.getUserId()).getCityId() == yinShan) {
            t1 = config.getParamConfig().getSuiTangCityMoveTime() * DateHelper.SECOND_MILLIONS * Math.max(2, config.getParamConfig().getSuiTangYinShanMoveCost());
        }

        long t2 = t1;

        // 获取称号buff
        int honoraryTitleBuffId = activity.getConfig().getParamConfig().getHonoraryTitleBuffId(user.getUserBaseInfo().getHonoraryTitle());
        int buffAddition = getBuffAddition(user.getUnionUid(), eSuiTangBuffType.Type1_TeamSpeedUp);
        if (honoraryTitleBuffId != 0 || buffAddition != 0) {
            // 速率+20%，  原时间 t1， 加速后时间 t2
            int buffEffect = getBuffEffect(honoraryTitleBuffId);
            buffEffect += buffAddition;
            BigDecimal buffEffectBD = new BigDecimal(buffEffect).add(BigDecimal.valueOf(1000));
            BigDecimal divisor = buffEffectBD.divide(BigDecimal.valueOf(1000));
            t2 = new BigDecimal(t1).divide(divisor, 0, BigDecimal.ROUND_DOWN).longValue();
        }

        // 自动加速次数
        int autoSpeedCnt = 0;
        // 加速档位
        int speedLimit = 0;

        team.setFromCityId(cityId);
        team.setCityId(toCityId);
        team.setMoveUpdateTime(now);

        synchronized (getPlayerLock(team.getUserId())) {
            team.setBeginMoveTime(now);
            team.setArriveTime(now + t2);

            // 处理下自动加速
            CrossSuiTangAutoSettingData autoSettingData = user.getAutoSettingData();

            // buff 效果
            int buffEffect = getBuffEffect(config.getParamConfig().getSuiTangCityMoveBuffId());
            // 加速限制
            speedLimit = Math.max(config.getParamConfig().getSuiTangCityMoveBuffCond(), autoSettingData.getSpeedLimit());
            while (autoSettingData != null
                    && autoSettingData.isAutoSpeed()
                    && autoSettingData.getIngotsTimes() > 0
                    && team.getArriveTime() - now > speedLimit * DateHelper.SECOND_MILLIONS) {
                long reduce = BigDecimal.valueOf(team.getArriveTime() - now).multiply(BigDecimal.valueOf(buffEffect)).
                        divide(BigDecimal.valueOf(1000)).
                        setScale(0, BigDecimal.ROUND_DOWN).longValue();
                long reduceBegin = BigDecimal.valueOf(now - team.getBeginMoveTime()).multiply(BigDecimal.valueOf(buffEffect)).
                        divide(BigDecimal.valueOf(1000)).
                        setScale(0, BigDecimal.ROUND_DOWN).longValue();
                reduce = reduce < 0 ? 0 : reduce;
                team.setBeginMoveTime(now - reduceBegin);
                team.setArriveTime(now + reduce);
                team.setSpeedUpNum(team.getSpeedUpNum() + 1);

                // 扣除下加速
                autoSpeedCnt++;
                autoSettingData.setIngotsTimes(autoSettingData.getIngotsTimes() - 1);
            }

            if (team.isAutoMove()) {
                team.setAutoBattle(true);
                team.setTargetCityId(toCityId);
            } else {
                team.setAutoBattle(false);
                team.setTargetCityId(0);
            }
        }

        // 加入目标城市指定队列
        CrossSuiTangCity toCity = curCityMap.get(toCityId);
        boolean isDef = user.getUnionUid().equals(toCity.getUnionId());
        synchronized (getCityLock(toCity)) {
            Map<Long, CrossSuiTangBattlefieldTeam> toTeamMap = isDef ? toCity.getBattlefield().getDefTeamMap() : toCity.getBattlefield().getAtkTeamMap();
            if (!isOut(team.getUserId())) {
                toTeamMap.put(team.getUserId(), new CrossSuiTangBattlefieldTeam(new CrossSuiTangTeamArrive(team.getUserId(), team.getArriveTime())));
            }
        }
        activity.getUserData(team.getUserId()).setUpdateOption();
        activity.getCrossSuiTangData().setUpdateOption();

        // 加入途径城市
        for (int cityIdTemp : routeList) {
            getCurCity(team.getUserId(), cityIdTemp).getMoveTeamList().put(team.getUserId(), team);
        }

        activity.getSyncActivityMgr().syncTeamToSelf(team);
        activity.getSyncActivityMgr().syncCity(city, false);
        activity.getSyncActivityMgr().syncBattleResultMsg(city);
        activity.getSyncActivityMgr().syncBattleResultMsg(toCity);

        // 加速消耗了多少需要告知游戏服
        if (autoSpeedCnt > 0) {
            // 元宝消耗额外记录一些数据
            CrossSuiTangUnionRound unionRoundData = getUnionRoundData(user.getUnionUid());
            // 元宝id#分组id#战场id#小组id#加速档位#城池id
            List<String> paramList = new ArrayList<>();
            paramList.add(GameConfig.GAME_MONEY_INGOTS + "");
            paramList.add(activity.getGroupId() + "");
            paramList.add(unionRoundData.getWarType().getType() + "");
            paramList.add(unionRoundData.getActGroupId() + "");
            paramList.add(speedLimit + "");
            paramList.add(toCityId + "");
            activity.getSyncActivityMgr().notifyConditionChange(team.getUserId(), eGamePlayerEventType.ConsumeItemRank, autoSpeedCnt, StringUtils.listToString(paramList, "#"));
        }

    }

    /*
     * 移动定时任务
     */
    public void crossSuiTangMoveTask(Map<Integer, CrossSuiTangCity> curCityMap) {
        try {
            // 清理已经到达目标城市的队伍移动列表
            Map<Long, CrossSuiTangTeam> syncMap = new ConcurrentHashMap<>();
            List<Long> removeList = new ArrayList<>();
            for (CrossSuiTangCity city : curCityMap.values()) {
                for (CrossSuiTangTeam team : city.getMoveTeamList().values()) {
                    if (System.currentTimeMillis() >= team.getArriveTime()) {
                        removeList.add(team.getUserId());
                        syncMap.put(team.getUserId(), team);
                    }
                }
                for (long id : removeList) {
                    city.getMoveTeamList().remove(id);
                }
            }
            for (CrossSuiTangTeam team : syncMap.values()) {
                CrossSuiTangUserData userData = activity.getUserData(team.getUserId());
                team.setSpeedUpNum(0);
                userData.setUpdateOption();
                activity.getSyncActivityMgr().syncUserDataToGameServer(userData);
                activity.getSyncActivityMgr().syncTeamToSelf(team);
                activity.getSyncActivityMgr().syncCity(getCity(team.getUserId()), false);
                activity.getSyncActivityMgr().syncBattleResultMsg(getCity(team.getUserId()));
                long userId = team.getUserId();
                if (!removeList.isEmpty()) {
                    if (nextMoveCity.containsKey(userId)) {
                        // 如果有下一个行军目标，直接向下一个进发
                        int ret = moveToCity(getTeam(userId), nextMoveCity.get(userId));
                        logger.debug("【隋唐】二阶段移动:{}", ret);
                        clearNextMoveCity(userId);
                    } else if (getTeam(userId).isAutoBattle()) {
                        //activity.addTask(new CrossSuiTangAutoBattleTask(activity, System.currentTimeMillis(), getCity(userId), getBattleTeam(getCity(userId), userId)));
                        //activity.getBattleActivityMgr().autoAttack(getCity(userId), getBattleTeam(getCity(userId), userId));
                    }
                }
            }

        } catch (Exception e) {
            logger.error("crossSuiTangMoveTask error", e);
        }
    }

    /*
     * 获取提前设定的行军目标
     */
    public CrossSuiTangCity getNextMoveCity(long userId) {
        if (nextMoveCity.containsKey(userId)) {
            return getCurCity(userId, nextMoveCity.get(userId));
        }
        return null;
    }

    /*
     * 清楚预设行军目标
     */
    public void clearNextMoveCity(long userId) {
        nextMoveCity.remove(userId);
    }

    /*
     * 寻找一个能走的城池
     */
    public int getCanMoveCity(CrossSuiTangTeam team) {
        Map<Integer, CrossSuiTangCity> curCityMap = getCurCityMap(team.getUserId());
        if (curCityMap == null) {
            return 0;
        }
        if (!getCurCity(team.getUserId(),team.getCityId()).getUnionId().equals(activity.getUserData(team.getUserId()).getUnionUid())
        && !isHomeCity(getCurCity(team.getUserId(),team.getCityId()), activity.getUserData(team.getUserId()).getUnionUid())) {
            return 0;
        }
        List<Integer> allCityId = new ArrayList<>();
        for (CrossSuiTangCity city : curCityMap.values()) {
            int ret = isLimitCityUserNum(team.getUserId(), city.getCityId());
            if (ret != 0) {
                continue;
            }
            if (city.getCityId() == team.getCityId() || city.getUnionId().equals(activity.getUserData(team.getUserId()).getUnionUid())) {
                continue;
            }
            List<Integer> routeList = getRouteList(team, city.getCityId());
            if (routeList != null && !routeList.isEmpty()) {
                allCityId.add(city.getCityId());
            }
        }
        if (!allCityId.isEmpty()) {
            return allCityId.get(randomHelper.next(0, allCityId.size()));
        }
        return 0;
    }

    /**
     * 获取当前队伍到指定城池的路径
     *
     * @param team
     * @param toCityId
     */
    private List<Integer> getRouteList(CrossSuiTangTeam team, int toCityId) {
        if (activity == null) {
            return null;
        }
        SuiTangConfigSync config = activity.getConfig();
        if (config == null) {
            return null;
        }
        // 目标城市为当前城市不可寻路
        if (team == null || team.getCityId() == toCityId) {
            return null;
        }
        CrossSuiTangData crossSuiTangData = activity.getCrossSuiTangData();
        if (crossSuiTangData == null) {
            return null;
        }

        SuiTangCityConfig toCityConfig = config.getCityConfigMap().get(toCityId);
        if (toCityConfig == null) {
            return null;
        }
        CrossSuiTangUserData userData = activity.getUserData(team.getUserId());
        SuiTangCityConfig curCityConfig = config.getCityConfigMap().get(team.getCityId());
        // 能走的格子，未处于战斗状态
        Map<Integer, List<Integer>> canMoveCityMap = new ConcurrentHashMap<>();
        Map<Integer, CrossSuiTangCity> curCityMap = getCurCityMap(userData.getUserId());
        if (curCityMap == null) {
            return null;
        }
        for (CrossSuiTangCity city : curCityMap.values()) {
            // 己方城池
            if (Objects.equals(city.getUnionId(), userData.getUnionUid())) {
                SuiTangCityConfig safeCityConfig = config.getCityConfigMap().get(city.getCityId());
                canMoveCityMap.put(safeCityConfig.getId(), config.getCityConnectMap().get(safeCityConfig.getId()));
            }
        }

        // 判断是否移除中心城
        int CenterCityType = eSuiTangCityType.CITY_TYPE_CENTER.getType();
        if (!activity.getCityActivityMgr().canOpenCenterCity()) {
            for (SuiTangCityConfig cfg : config.getCityConfigMap().values()) {
                if (cfg.getType() == CenterCityType) {
                    canMoveCityMap.remove(cfg.getId());
                    if (toCityId == cfg.getId()) {
                        return null;
                    }
                }
            }
        }
        // 判断是否移除阴山
        int YinShanCityType = eSuiTangCityType.CITY_TYPE_YINSHAN.getType();
        if (!activity.getCityActivityMgr().canOpenYinShanCity(getCurUserRound(userData.getUserId()).getWarType(), getCurUserRound(userData.getUserId()).getActGroupId())) {
            for (SuiTangCityConfig cfg : config.getCityConfigMap().values()) {
                if (cfg.getType() == YinShanCityType) {
                    canMoveCityMap.remove(cfg.getId());
                    if (toCityId == cfg.getId()) {
                        return null;
                    }
                }
            }
        }

        // 起点强制加进去
        canMoveCityMap.put(curCityConfig.getId(), config.getCityConnectMap().get(curCityConfig.getId()));
        return SuiTangMapLogic.getRouteList(canMoveCityMap, curCityConfig.getId(), toCityConfig.getId(), isMyCity(userData));
    }

    /*
     * isMyCity
     */
    public boolean isMyCity(CrossSuiTangUserData userData) {
        if (userData == null) {
            return false;
        }
        CrossSuiTangCity city = getCity(userData.getUserId());
        return city != null && city.getUnionId().equals(userData.getUnionUid());
    }


    /**
     * 计算下次到达时间 格子 * 单位时间
     */
    public long calcMoveTime(List<Integer> routeList) {
        if (activity == null) {
            return 0;
        }
        SuiTangConfigSync config = activity.getConfig();
        if (config == null) {
            return 0;
        }
        return (long) config.getParamConfig().getSuiTangCityMoveTime() * DateHelper.SECOND_MILLIONS * SuiTangMapLogic.CalcRouteCost(routeList);
    }
}
